﻿#pragma once
#include "sort.h"
void print(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
}
void swap(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}
void SelectSort(int* a, int n)//选择排序
{
	int left = 0, right = n - 1;
	while (left < right)
	{
		//int max = 0, mini = 0;
		//这里的max和mini必须在left和right之间，由于right和left都在动，不能把max和mini都设置为0
		int max = left, mini = left;
		for (int i = left; i <= right; i++)
		{
			if (a[i] > a[max])
			{
				max = i;
			}
			if (a[i] < a[mini])
			{
				mini = i;
			}
		}
		swap(&a[left], &a[mini]);
		if (max == left)
		{
			max = mini;
		}
		swap(&a[right], &a[max]);
		left++;
		right--;
	}
}

void AdjustDown(int* a, int n, int parent)//向下调整
{
	int child = 2 * parent + 1;
	while (child < n)
	{
		if (child + 1 < n && a[child + 1] > a[child])
		{
			child++;
		}
		if (a[parent] < a[child])
		{
			swap(&a[parent], &a[child]);
			parent = child;
			child = 2 * parent + 1;
		}
		else
			break;
	}
}

void Heapsort(int* a, int n)//堆排序
{
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(a, n, i);
	}
	int end = n - 1;
	while (end > 0)
	{
		swap(&a[end], &a[0]);
		AdjustDown(a, end, 0);
		end--;
	}
}

void Bubblesort(int* a, int n)
{
	for (int j = 0; j < n;j++)
	{
		bool exchange = false;
		for (int i = 1; i < n - j; i++)
		{
			if (a[i - 1] > a[i])
			{
				swap(&a[i - 1], &a[i]);
				exchange = true;
			}
		}
		if (exchange == false)
			break;
	}
}

void Quicksort1(int* a, int left,int right)//hoare法快速排序
{
	if (left >= right)
		return;
	int begin = left, end = right;
	int randi = left + (rand() % (right - left));
	swap(&a[left], &a[randi]);
	//hoare法部分↓
	int keyi = left;
	while (left < right)
	{
		while (left < right && a[right] >= a[keyi])
		{
			right--;
		}
		while (left < right && a[left] <= a[keyi])
		{
			left++;
		}
		swap(&a[left], &a[right]);
	}
	swap(&a[keyi], &a[left]);
	keyi = left;
	//hoare法部分↑
	Quicksort1(a, begin, keyi - 1);
	Quicksort1(a, keyi + 1, end);
}

void Insertsort(int* a,int n)//插入排序
{
	for (int i = 1; i < n; i++)
	{
		int end = i - 1;
		int tmp = a[i];
		while (end >= 0)
		{
			if (a[end] > tmp)
			{
				a[end + 1] = a[end];
				end--;
			}
			else
				break;
		}
		a[end + 1] = tmp;
	}
}
void Quicksort2(int* a, int left, int right)//挖坑法快速排序(使用插入排序辅助)
{
	if (left >= right)
		return;
	int begin = left, end = right;
	int randi = left + (rand() % (right - left));
	swap(&a[left], &a[randi]);
	//挖坑法的部分↓
	int key = a[left];
	int holei = left;
	while (left < right)
	{
		while (left < right && a[right] >= key)
		{
			right--;
		}
		a[holei] = a[right];
		holei = right;
		while (left < right && a[left] <= key)
		{
			left++;
		}
		a[holei] = a[left];
		holei = left;
	}
	a[holei] = key;
	//挖坑法部分↑
	Quicksort2(a, begin, holei - 1);
	Quicksort2(a, holei + 1, end);
}

void Quicksort3(int* a, int left, int right)//前后指针快速排序
{
	if (left >= right)
		return;
	int begin = left, end = right;
	int randi = left + (rand() % (right - left));
	swap(&a[left], &a[randi]);
	//前后指针的部分↓
	int prev = left, keyi = left;
	int cur = prev + 1;
	while (cur <= right)
	{
		if (a[cur] < a[keyi] && ++prev != cur)
			swap(&a[prev], &a[cur]);
		++cur;
	}
	swap(&a[keyi], &a[prev]);
	keyi = prev;
	//前后指针法部分↑
	Quicksort3(a, begin, keyi - 1);
	Quicksort3(a, keyi + 1, end);
}

void Quicksort4(int* a, int left, int right)//小区间优化快速排序
{
	if (left >= right)
		return;
	if (right - left + 1 > 10)
	{
		int begin = left, end = right;
		int randi = left + (rand() % (right - left));
		swap(&a[left], &a[randi]);
		//前后指针的部分↓
		int prev = left, keyi = left;
		int cur = prev + 1;
		while (cur <= right)
		{
			if (a[cur] < a[keyi] && ++prev != cur)
				swap(&a[prev], &a[cur]);
			++cur;
		}
		swap(&a[keyi], &a[prev]);
		keyi = prev;
		//前后指针法部分↑
		Quicksort4(a, begin, keyi - 1);
		Quicksort4(a, keyi + 1, end);
	}
	else
		Insertsort(a + left, right - left + 1);
}